<?php 


require_once __DIR__ . '/Err.php';
/**
* 
*/
class Article
{
	
	/**
	*PDO连接对象
	*@param  $db   PDO
	**/
	private $_db;

	function __construct(PDO $db)
	{
		$this->_db = $db;
	}


	/**
	* 文章发表
	*@param var $title  文章名
	*@param var $content  文章内容
	*@param var $user_id  发表文章用户的id
	* return array
	**/

	public function articleCreate($title,$content,$user_id){
		// print_r($this->_db);exit();
		if (empty($title)) {
			throw new Exception("文章名不能为空", Err::ARTICLE_NAME_NOT_NULL);
			
		}
		if (empty($content)) {
			throw new Exception("文章内容不能为空", Err::ARTICLECONTENT_NOT_NULL);
			
		}

		if (empty($user_id)) {
			throw new Exception("用户id不能为空", Err::ARTICLE_ID_NOT_NULL);
			
		}
		$create_time = date('Y-m-d H:i:s',time());
		$sql = "insert into article (title,content,user_id,create_time) values (:title,:content,:user_id,:create_time)";
		$sm = $this->_db->prepare($sql);
		$sm->bindParam(':title',$title);
		$sm->bindParam(':content',$content);
		$sm->bindParam(':user_id',$user_id);
		$sm->bindParam('create_time',$create_time);
		if (!$sm->execute()) {
			throw new Exception("文章发表失败", Err::ARTICLE_CREATE_FAIL);
			
		}

		return [
			'title'=>$title,
			'content'=>$content,
			'user_id'=>$user_id,
			'article_id'=>$this->_db->lastInsertId(),
			'create_time'=>$create_time
		];
		
	}


	/**
	*文章查看
	*@param var $title  文章id
	*return array() 
	**/
	public function view($article_id) {
		if (empty($article_id)) {
			throw new Exception("文章id不能为空", Err::ARTICLE_ID_CONNOT_NULL);
			
		}

		$sql = "select * from article where ( id = :article_id )";
		$sm = $this->_db->prepare($sql);
		
		$sm->bindParam(':article_id',$article_id);
		// var_dump($sm->execute());exit();
		// print_r($sm);exit();
		if (!$sm->execute()) {
			throw new Exception("文章查询失败", Err::ARTICLE_SHOW_FAIL);
			
		}
		
		$rm = $sm->fetch(PDO::FETCH_ASSOC);
		
		if (empty($rm)) {
			throw new Exception("文章不存在", Err::ARTICLE_NOT_EXIST);
			
		}

		return $rm;
	}

	/**
	*文章修改
	*@param int $article_id 文章id
	*@param var $title      文章标题
	*@param var $content    文章内容
	*@param var $user_id    文章发表者id
	**/

	public function edit($article_id,$title,$content,$user_id) {


		if (empty($article_id)) {
			throw new Exception("文章id不能为空", ARTICLE_ID_CONNOT_NULL);
			
		}
		$article = $this->view($article_id);

		
		if (($user_id !== (int)$article['user_id'])) {
			throw new Exception("你无权操作此文章", Err::ARTICLE_NOT_POWER);
			
		}
		$title = empty($title) ? $article['title'] : $title;
		$content = empty($content) ? $article['content'] : $content;
		if ($title == $article['title'] && $content == $article['content']) {
			return $article;
			
		}

		$sql = "update article set title = :title , content = :content where (id = :id)";
		$sm = $this->_db->prepare($sql);
		$sm->bindParam(':title',$title);
		$sm->bindParam(':content',$content);
		$sm->bindParam(':id',$article_id);
		if(!$sm->execute()){
			throw new Exception("文章修改失败", Err::ARTICLE_EDIT_FAIL);
			
		}

		return [
			'title'=>$title,
			'content'=>$content,
			'user_id'=>$user_id
		];


	}


	/**
	* 文章删除

	**/
	public function delete($article_id,$user_id){
		if (empty($article_id)) {
			throw new Exception("文章id不能为空", ARTICLE_ID_CONNOT_NULL);
			
		}

		if (empty($user_id)) {
			throw new Exception("用户id不能为空", ARTICLE_USER_CONNOT_NULL);
			
		}
		$article = $this->view($article_id);

		
		if (($user_id !== (int)$article['user_id'])) {
			throw new Exception("你无权操作此文章", Err::ARTICLE_NOT_POWER);
			
		}

		$sql = "delete from article where id = :article_id";
		$sm = $this -> _db->prepare($sql);
		$sm->bindParam(':article_id',$article_id);
		if(!$sm->execute()){
			throw new Exception("删除错误", Err::ARTICLE_DELETE_FAIL);
			
		}

		return [
			'message'=>'删除成功',
			'code'=>200
		];

		
	}

	public function _list(){

	}
}

 ?>